package cn.yunhe.dplayerdemo3.controller;

import cn.yunhe.dplayerdemo3.Service.IDanmakuService;
import cn.yunhe.dplayerdemo3.entity.Danmup;
import cn.yunhe.dplayerdemo3.util.TransformDate;
import cn.yunhe.dplayerdemo3.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DanMupController {
    @Autowired
    private IDanmakuService danmakuService;

    @RequestMapping("/test")
    public ModelAndView test() {
        return new ModelAndView("test");
    }

    /**
     * }
     * 添加弹幕信息
     *
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/addDanmup")
    public Danmup addDanmup(HttpServletRequest request) {
        try {
            String danmu = request.getParameter("danmu");
            String d[] = danmu.split(",");

            String text = d[0].split(":")[1].substring(1, d[0].split(":")[1].length() - 1);
            System.out.println(text);
            String color = d[1].split(":")[1].substring(1, d[1].split(":")[1].length() - 1);
            System.out.println(color);
            int size = Integer.parseInt(d[2].split(":")[1].substring(1, d[2].split(":")[1].length() - 1));
            System.out.println(size);
            int position = Integer.parseInt(d[3].split(":")[1].substring(1, d[3].split(":")[1].length() - 1));
            System.out.println(position);
            String time = d[4].split(":")[1].substring(0, d[4].split(":")[1].length() - 1);
            System.out.println(time);
            Danmup danmup = new Danmup(text, color, size, position, time, 1);
            System.out.println(danmu);


            String dateTime = TransformDate.formatSecondDateTime(Integer.parseInt(time));
            Danmup danmup1 = new Danmup(text, color, size, position, dateTime, 1);

            if (danmakuService.save(danmup)) {
                WebSocket webSocket = new WebSocket();
                webSocket.sendMessage(danmup1);

                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("添加弹幕方法执行");
        return null;
    }


    /**
     * 接收弹幕
     *
     * @return
     */
    @RequestMapping("/findDanmups")
    public String findDanmups(Model model) {
        List<Danmup> danmupList = danmakuService.getDanmu(1);
        System.out.println(danmupList.size());
        String jsonString = "[";
        if (danmupList.size() != 0 && danmupList != null) {
            if (danmupList.size() == 1) {
                jsonString = jsonString + "\'{\"text\":\""
                        + danmupList.get(0).getText() + "\",\"color\":\""
                        + danmupList.get(0).getColor() + "\",\"size\":\""
                        + danmupList.get(0).getSize() + "\",\"position\":\""
                        + danmupList.get(0).getPosition() + "\",\"time\":"
                        + danmupList.get(0).getTime() + "}\']";
            } else {
                for (int i = 0, len = danmupList.size(); i < len; i++) {
                    if (i == len - 1) {
                        jsonString = jsonString + "\'{\"text\":\""
                                + danmupList.get(i).getText() + "\",\"color\":\""
                                + danmupList.get(i).getColor() + "\",\"size\":\""
                                + danmupList.get(i).getSize() + "\",\"position\":\""
                                + danmupList.get(i).getPosition() + "\",\"time\":"
                                + danmupList.get(i).getTime() + "}\']";
                    } else {
                        jsonString = jsonString + "\'{\"text\":\""
                                + danmupList.get(i).getText() + "\",\"color\":\""
                                + danmupList.get(i).getColor() + "\",\"size\":\""
                                + danmupList.get(i).getSize() + "\",\"position\":\""
                                + danmupList.get(i).getPosition() + "\",\"time\":"
                                + danmupList.get(i).getTime() + "}\',";
                    }
                }
            }
        }
        model.addAttribute(danmupList);
        System.out.println(jsonString);
        return jsonString;
    }



    @RequestMapping("/formateDate")
    public String formateDate() {
        String dateTime = TransformDate.formatSecondDateTime(1343);
        return dateTime;
    }
}
